<?php
add_action('widgets_init', 'register_top_store_home_widget'); // load widget

function register_top_store_home_widget() { // register widget
    register_widget('top_store_home');
}

class top_store_home extends WP_Widget {// widget class {

    function top_store_home() { 
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Display top Store Home page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'top_store_home');
        $this->WP_Widget('top_store_home', 'Coupon 6789 - top Stores Home page', $widget_ops, $control_ops);
    }

    function widget($args, $instance) { // display widget
        global $wpdb,$table_prefix;
        $title = apply_filters('widget_title', $instance['title']);
        $countItem = $instance['count_item'];
        $row = 4;
        $cell = 10;
        if($countItem == ''){
            $countItem = 100;
        }
        //get ids store
        $args = array(
                'post_type' => 'store',
                'post_status' =>'publish',
                'posts_per_page'=> $countItem,
                'orderby' => 'meta_value',
                'meta_key' => 'count_coupon_in_store',
                'order' =>'DESC'
            );
            $query = new WP_Query($args);
            $stores = $query->posts;
            $ids_arr = array();
            foreach ($stores as $st){
                array_push($ids_arr, $st->ID);
            }
            $categories = get_terms('store_category',array('hide_empty'=> false));
        ?>
        <div class="textlink">
             <div class="ti"><span id="down"></span><span id="up"></span><p><?php echo $title;?></p></div>
             <ul id="linkss">
                 <?php
                    foreach($categories as $categorys){
                    $args = array(
                            'post_type' => 'store',
                            'post_status' =>'publish',
                            'post__in' => $ids_arr,
                            'meta_query' => array(
                                'relation' => 'AND',
                                array(
                                   'key' => 'category_store_metadata',
                                   'value' => $categorys->name,
                                   'compare' => 'LIKE'
                                 ),
                            ),
                            'orderby' => 'meta_value',
                            'meta_key' => 'count_coupon_in_store',
                            'order' =>'desc'
                        );
                   $query = new WP_Query( $args );
                   $stores = $query->posts;
                   if($stores != null){ ?>
                   <li>
                        <a href="#"><?php echo $categorys->name?></a>
                        <ul>
                            <?php
                                foreach ($stores as $st){
                                    $old_count = $wpdb->get_var("select meta_value from ".$table_prefix."postmeta where meta_key = 'count_coupon_in_store'
                                        AND post_id = $st->ID"); ?>
                                    <li><a href="<?php echo get_permalink($st->ID);?>"><?php echo $st->post_title;?></a></li>
                               <?php }
                            ?>
                        </ul>
                    </li>
               <?php }
                 ?>
                <?php
                    }
                 ?>
             </ul>
        </div>
        <?php
    }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['count_item'] = $new_instance['count_item'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        ?>
        <div style="color: #333;">
            <p>        
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
            <p>
    		<label for="<?php echo $this->get_field_id('count_item');?>"><?php echo 'Enter number top store display';?></label>
    		<input type="text" id="<?php echo $this->get_field_id('count_item');?>" name="<?php echo $this->get_field_name('count_item');?>" value="<?php echo $instance['count_item'];?>" style="width:90%;" />
            </p>
        </div>
        <?php
    }

}
?>
